import {
    Actor,
    HttpAgent,
    // IDL,
    ActorSubclass,
} from '@dfinity/agent';
// import { InterfaceFactory } from '@dfinity/candid/lib/cjs/idl';

// Canister ID: index canister
const canisterId = 'mxzaz-hqaaa-aaaar-qaada-cai';
import { idlFactory } from "./icrc1_ledger_canister_backend.ts"; // 假设您已生成并导入IDL


// Initialize an agent to interact with the canister
const agent = new HttpAgent({
    host: 'http://localhost:4943', // Ensure this matches the network you're interacting with
});

// Uncomment this line if you are using a local replica
agent.fetchRootKey();

// Create an actor with the specified canister ID and IDL
const canisterActor: ActorSubclass = Actor.createActor(idlFactory as any, {
    agent,
    canisterId,
});

async function get_account_transactions(start: number, length: number) {
    try {
        const result: any = await canisterActor.get_transactions({
            start,
            length
        });
        if (result) {
            // const mintTransactions = result.Ok.transactions.filter(
            //     (tx: any) => tx.transaction.kind === "mint"
            // ).map((tx: any) => ({
            //     id: tx.id,
            //     amount: tx.transaction.mint[0].amount,
            //     timestamp: tx.transaction.timestamp,
            // }));;
            console.log(`transactions are:`, result.transactions);
            return result.transaction;
        } else {
            console.log("Error:");
        }
    } catch (error) {
        console.error("Error fetching balance:", error);
        throw error;
    }
}

get_account_transactions(1, 1);
